<?php
// Only those with full privs can do configuration
if($fullPower) {
?>
<script language="javascript">
// This adds a new email address to the email list textarea
function addEmail() {
  var email = document.getElementById("email").value;
  var emailList = document.getElementById("emails");
  
  // Validate the email address
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if(!filter.test(email)) {
    alert("Please enter a valid email address");
    return false;
  }
  
  // Do not allow duplicate email addresses
  var emailArray = emailList.value.split("\n");
  for(i=0; i<emailArray.length; i++) {
    if(emailArray[i] == email) {
      alert(email + " is already in the list");
      return false;
    }
  }
  
  // Add a new line if there is more than 1 email
  if(emailList.value.length > 1) {
    emailList.value += "\n";
  }
  
  emailList.value += email;
}

// Generate a random hash for the user
function generateHash() {
  var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 16;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	document.getElementById("hash").value = randomstring;
}

// Javascript will do all the field checking before sending it off for processing
function formVerify() {
  var noWarnings = true;
  var warningMessage = "";
  
  // Demo dir check
  if(document.getElementById("demoDir").value == "") {
    warningMessage += "You must specify a directory for demos.\n";
    document.getElementById("demoDirWarn").style.display = "";
    noWarnings = false;
  } else {
    document.getElementById("demoDirWarn").style.display = "none";
  }
  
  // Demo Size check
  if(document.getElementById("demoSizeLimit").value == "") {
    warningMessage += "You must specify a demo size limit.\n";
    document.getElementById("demoSizeLimitWarn").style.display = "";
    noWarnings = false;
  } else {
    document.getElementById("demoSizeLimitWarn").style.display = "none";
  }
  
  // Ban Message
  if(document.getElementById("banMessage").value == "") {
    warningMessage += "You must specify ban message.\n";
    document.getElementById("banMessageWarn").style.display = "";
    noWarnings = false;
  } else {
    document.getElementById("banMessageWarn").style.display = "none";
  }
  
  // Ban Appeal Site
  if(document.getElementById("banAppealSite").value == "") {
    warningMessage += "You must specify site for banned users to appeal to.\n";
    document.getElementById("banAppealSiteWarn").style.display = "";
    noWarnings = false;
  } else {
    document.getElementById("banAppealSiteWarn").style.display = "none";
  }
  
  // Days to keep pending banned
  if(document.getElementById("daysBanPending").value == "") {
    warningMessage += "You must specify the number of days to keep a pending ban banned for.\n";
    document.getElementById("daysBanPendingWarn").style.display = "";
    noWarnings = false;
  } else {
    document.getElementById("daysBanPendingWarn").style.display = "none";
  }
  
  // Hash
  if(document.getElementById("hash").value == "") {
    warningMessage += "You must specify a hash code for security reasons.\n";
    document.getElementById("hashWarn").style.display = "";
    noWarnings = false;
  } else {
    document.getElementById("hashWarn").style.display = "none";
  }
  
  // SMF
  if(document.getElementById("smfIntegration").value == "true") {
    // Full Power Users
    if(document.getElementById("smfFullPowerGroup").value == "" || document.getElementById("smfFullPowerGroup").value == 0) {
      warningMessage += "You must specify a SMF group that will have full power privileges.\n";
      document.getElementById("smfFullPowerGroupWarn").style.display = "";
      noWarnings = false;
    } else {
      document.getElementById("smfFullPowerGroupWarn").style.display = "none";
    }
    // Ban Managers
    if(document.getElementById("smfBanManagerGroup").value == "" || document.getElementById("smfBanManagerGroup").value == 0) {
      warningMessage += "You must specify a SMF group that will have ban manager privileges.\n";
      document.getElementById("smfBanManagerGroupWarn").style.display = "";
      noWarnings = false;
    } else {
      document.getElementById("smfBanManagerGroupWarn").style.display = "none";
    }
    // Admins
    if(document.getElementById("smfAdminGroup").value == "" || document.getElementById("smfAdminGroup").value == 0) {
      warningMessage += "You must specify a SMF group that will have admin privileges.\n";
      document.getElementById("smfAdminGroupWarn").style.display = "";
      noWarnings = false;
    } else {
      document.getElementById("smfAdminGroupWarn").style.display = "none";
    }
    // Members
    if(document.getElementById("smfMemberGroup").value == "" || document.getElementById("smfMemberGroup").value == 0) {
      warningMessage += "You must specify a SMF group that will have member privileges.\n";
      document.getElementById("smfMemberGroupWarn").style.display = "";
      noWarnings = false;
    } else {
      document.getElementById("smfMemberGroupWarn").style.display = "none";
    }
    document.getElementById("createUserCodeWarn").style.display = "none";
    document.getElementById("createSuperCodeWarn").style.display = "none";
  } else {
    // Create User Code Check
    if(document.getElementById("createUserCode").value == "") {
      warningMessage += "You must specify user creation code.\n";
      document.getElementById("createUserCodeWarn").style.display = "";
      noWarnings = false;
    } else {
      document.getElementById("createUserCodeWarn").style.display = "none";
    }

    // Create Super User Code Check
    if(document.getElementById("createSuperCode").value == "") {
      warningMessage += "You must specify a super user creation code.\n";
      document.getElementById("createSuperCodeWarn").style.display = "";
      noWarnings = false;
    } else {
      document.getElementById("createSuperCodeWarn").style.display = "none";
    }
    document.getElementById("smfFullPowerGroupWarn").style.display = "none";
    document.getElementById("smfBanManagerGroupWarn").style.display = "none";
    document.getElementById("smfAdminGroupWarn").style.display = "none";
    document.getElementById("smfMemberGroupWarn").style.display = "none";
  }

  if(!noWarnings) {
    alert(warningMessage);
  }
  
  return noWarnings;
}
</script>

<form action="index.php?page=configurationSave" onsubmit="return formVerify();" method="POST">
  <div class="tborder">
    <div id="tableHead">
      <div><b>Website Settings</b></div>
    </div>

    <table class="bordercolor" width="100%" cellspacing="1" cellpadding="5" border="0" style="margin-top: 1px;">
    <tr>
      <td class="rowColor1" width="1%" nowrap>Site Name:</td>
      <td class="rowColor1" width="1%"><input type="text" name="siteName" value="<?=$config->siteName?>" size="40" maxlength="255"/></td>
      <td class="rowColor1" width="1%" nowrap>Logo:</td>
      <td class="rowColor1" width="1%"><input type="text" name="logo" value="<?=$config->siteLogo?>" size="40" maxlength="100"/></td>
    </tr>
    <tr>
      <td class="rowColor2" width="1%" nowrap>Enable Forum Link:</td>
      <td class="rowColor2" width="1%">
        <select name="enableForumLink">
          <option value="true"<?php if($config->enableForumLink) echo " selected"; ?>>Yes</option>
          <option value="false"<?php if(!$config->enableForumLink) echo " selected"; ?>>No</option>
        </select>
      </td>
      <td class="rowColor2" width="1%" nowrap>Forum Address:</td>
      <td class="rowColor2" width="1%"><input type="text" name="forumURL" value="<?=$config->forumURL?>" size="40" maxlength="255"/></td>
    </tr>
    <tr>
      <td class="rowColor1" width="1%" nowrap>Bans Per Page:</td>
      <td class="rowColor1" width="1%"><input type="text" name="bansPerPage" value="<?=$config->bansPerPage?>" size="10" maxlength="5" onkeyup="removeCharacters(this)"/></td>
      <td class="rowColor1" width="1%" nowrap>Number of Page Links:</td>
      <td class="rowColor1" width="1%"><input type="text" name="numPageLinks" value="<?=$config->maxPageLinks?>" size="5" maxlength="2" onkeyup="removeCharacters(this)"/></td>
    </tr>
    <tr>
      <td class="rowColor2" width="1%" nowrap>Demo Directory:</td>
      <td class="rowColor2" width="1%">
        <input type="text" id="demoDir" name="demoDir" value="<?=$config->demoRootDir?>" size="40" maxlength="40"/>
        <img src="images/warning.png" id="demoDirWarn" style="display:none"/>
      </td>
      <td class="rowColor2" width="1%" nowrap>Demo Size Limit(MB):</td>
      <td class="rowColor2" width="1%">
        <input type="text" id="demoSizeLimit" name="demoSizeLimit" value="<?=$config->demoSizeLimit?>" size="10" maxlength="5" onkeyup="removeCharacters(this)"/>
        <img src="images/warning.png" id="demoSizeLimitWarn" style="display:none"/>
      </td>
    </tr>
    <tr>
      <td class="rowColor1" width="1%" nowrap>User Create Code:</td>
      <td class="rowColor1" width="1%">
        <input type="text" id="createUserCode" name="createUserCode" value="<?=$config->createUserCode?>" size="20" maxlength="30"/>
        <img src="images/warning.png" id="createUserCodeWarn" style="display:none"/>
      </td>
      <td class="rowColor1" width="1%" nowrap>Super User Create Code:</td>
      <td class="rowColor1" width="1%">
        <input type="text" id="createSuperCode" name="createSuperCode" value="<?=$config->createSuperCode?>" size="20" maxlength="30"/>
        <img src="images/warning.png" id="createSuperCodeWarn" style="display:none"/>
      </td>
    </tr>
    <tr>
      <td class="rowColor2" width="1%" nowrap>Send Emails On Ban:</td>
      <td class="rowColor2" width="1%">
        <select name="sendEmailsOnBan">
          <option value="true"<?php if($config->sendEmails) echo " selected"; ?>>Yes</option>
          <option value="false"<?php if(!$config->sendEmails) echo " selected"; ?>>No</option>
        </select>
      </td>
      <td class="rowColor2" width="1%" nowrap>Send Emails On Demo Add:</td>
      <td class="rowColor2" width="1%">
        <select name="sendEmailsDemo">
          <option value="true"<?php if($config->sendDemoEmails) echo " selected"; ?>>Yes</option>
          <option value="false"<?php if(!$config->sendDemoEmails) echo " selected"; ?>>No</option>
        </select>
      </td>
    </tr>
    <tr>
      <td class="rowColor1" width="1%" nowrap>Email Address of Sender:</td>
      <td class="rowColor1" width="1%" colspan="3"><input type="text" name="senderEmail" value="<?=$config->emailFromHeader?>" size="40" maxlength="255"/></td>
    </tr>
    <tr>
      <td class="rowColor1" width="1%" nowrap valign="top">Email Addresses to Recieve Ban and Demo Notices:</td>
      <td class="rowColor1" width="1%" valign="top">
        <input type="text" id="email" name="email" value="" size="40" maxlength="255"/>
        <input type="button" value="Add >>" onclick="addEmail()"/>
      </td>
      <td class="rowColor1" width="1%" nowrap colspan="2">
        <textarea id="emails" name="emails" rows="5" cols="40" readonly><?php
            for($i=0; $i<count($config->banManagerEmails); $i++) {
              echo $config->banManagerEmails[$i];
              if($i<count($config->banManagerEmails)-1) {
                echo "\n";
              }
            }
          ?></textarea>
      </td>
    </tr>
    </table>
  </div>
  
  <br/>
  
  <div class="tborder">
    <div id="tableHead">
      <div><b>Ban Settings</b></div>
    </div>

    <table class="bordercolor" width="100%" cellspacing="1" cellpadding="5" border="0" style="margin-top: 1px;">
    <tr>
      <td class="rowColor1" width="1%" nowrap>Ban Message:</td>
      <td class="rowColor1" width="1%">
        <input type="text" id="banMessage" name="banMessage" value="<?=$config->banMessage?>" size="60" maxlength="255"/>
        <img src="images/warning.png" id="banMessageWarn" style="display:none"/>
      </td>
      <td class="rowColor1" width="1%" nowrap>Ban Appeal Site:</td>
      <td class="rowColor1" width="1%">
        <input type="text" id="banAppealSite" name="banAppealSite" value="<?=$config->appealSite?>" size="40" maxlength="255"/>
        <img src="images/warning.png" id="banAppealSiteWarn" style="display:none"/>
      </td>
    </tr>
    <tr>
      <td class="rowColor2" width="1%" nowrap>Days to keep pending bans banned:</td>
      <td class="rowColor2" width="1%">
        <input type="text" id="daysBanPending" name="daysBanPending" value="<?=$config->daysBanPending?>" size="10" maxlength="5" onkeyup="removeCharacters(this)"/>
        <img src="images/warning.png" id="daysBanPendingWarn" style="display:none"/>
      </td>
      <td class="rowColor2" width="1%" nowrap>Allow Admins to be Banned:</td>
      <td class="rowColor2" width="1%">
        <select name="allowAdminBan">
          <option value="true"<?php if($config->allowAdminBans) echo " selected"; ?>>Yes</option>
          <option value="false"<?php if(!$config->allowAdminBans) echo " selected"; ?>>No</option>
        </select>
      </td>
    </tr>
    <tr>
      <td class="rowColor2" width="1%" nowrap>Hash Code:</td>
      <td class="rowColor2" width="1%">
        <input type="text" id="hash" name="hash" value="<?=$config->matchHash?>" size="40" maxlength="255"/>
        <img src="images/warning.png" id="hashWarn" style="display:none"/>
        <input type="button" value="Generate" onclick="generateHash()">
      </td>
      <td class="rowColor2" width="1%" nowrap>Teach Admins:</td>
      <td class="rowColor2" width="1%">
        <select name="teachAdmins">
          <option value="1"<?php if($config->teachAdmins == 1) echo " selected"; ?>>Yes</option>
          <option value="0"<?php if($config->teachAdmins == 0) echo " selected"; ?>>No</option>
        </select>
      </td>
    </tr>
    </table>
  </div>
  
  <br/>

  <div class="tborder">
    <div id="tableHead">
      <div><b>SMF Integration Settings</b></div>
    </div>

    <table class="bordercolor" width="100%" cellspacing="1" cellpadding="5" border="0" style="margin-top: 1px;">
    <tr>
      <td class="rowColor1" width="1%" nowrap>Enable SMF Integration:</td>
      <td class="rowColor1" width="1%" colspan="3">
        <select id="smfIntegration" name="smfIntegration">
          <option value="true"<?php if($config->enableSmfIntegartion) echo " selected"; ?>>Yes</option>
          <option value="false"<?php if($config->enableSmfIntegartion) echo " selected"; ?>>No</option>
        </select>
      </td>
    </tr>
    <tr>
      <td class="rowColor2" width="1%" nowrap>SMF Full Power Group:</td>
      <td class="rowColor2" width="1%">
        <input type="text" id="smfFullPowerGroup" name="smfFullPowerGroup" value="<?=$config->fullPowerGroup?>" size="10" maxlength="5" onkeyup="removeCharacters(this)"/>
        <img src="images/warning.png" id="smfFullPowerGroupWarn" style="display:none"/>
      </td>
      <td class="rowColor2" width="1%" nowrap>SMF Ban Manger Group:</td>
      <td class="rowColor2" width="1%">
        <input type="text" id="smfBanManagerGroup" name="smfBanManagerGroup" value="<?=$config->banManagerGroup?>" size="10" maxlength="5" onkeyup="removeCharacters(this)"/>
        <img src="images/warning.png" id="smfBanManagerGroupWarn" style="display:none"/>
      </td>
    </tr>
    <tr>
      <td class="rowColor1" width="1%" nowrap>SMF Admin Group:</td>
      <td class="rowColor1" width="1%">
        <input type="text" id="smfAdminGroup" name="smfAdminGroup" value="<?=$config->adminGroup?>" size="10" maxlength="5" onkeyup="removeCharacters(this)"/>
        <img src="images/warning.png" id="smfAdminGroupWarn" style="display:none"/>
      </td>
      <td class="rowColor1" width="1%" nowrap>SMF Member Group:</td>
      <td class="rowColor1" width="1%">
        <input type="text" id="smfMemberGroup" name="smfMemberGroup" value="<?=$config->memberGroup?>" size="10" maxlength="5" onkeyup="removeCharacters(this)"/>
        <img src="images/warning.png" id="smfMemberGroupWarn" style="display:none"/>
      </td>
    </tr>
    </table>
  </div>

  <br/>
  
  <div class="tborder">
    <div id="tableHead">
      <div><input type="submit" value="Save Configuration"></div>
    </div>
  </div>

  <h5>Note: Saving the configuration will also update GlobalBan.cfg on all active servers.</h5>

</form>

<?php
} else {
?>
<div class="tborder">
  <div id="tableHead">
    <div><b>Access Denied</b></div>
  </div>
<div class="tborder">
<?php
}
?>